package edu.gmu.client;

import edu.gmu.messages.EchoResponse;
import edu.gmu.messages.EchoRequest;
import edu.gmu.middleware.ClientDispatcher;
import edu.gmu.middleware.MessageEvent;
import edu.gmu.middleware.MessageListener;
import edu.gmu.middleware.Sink;
import edu.gmu.middleware.SinkFactory;
import java.io.IOException;

public class EchoClient
{
 
    public static void main( String[] args ) throws IOException, InterruptedException
    {
        String remoteAddress = "localhost";
        int remotePort       = 5000;
        int localPort        = Integer.parseInt(args[0]);
        
        Sink sink = SinkFactory.makeUDPClient(  remoteAddress,
                                                remotePort,
                                                localPort);
        
        final ClientDispatcher dispatcher = new ClientDispatcher(sink, 4, "EchoClient");
        
        
        
        dispatcher.addMessageListener( new MessageListener() 
        {
            public void messageReceived(MessageEvent event)
            {
                EchoResponse m = (EchoResponse)event.getMessage();
                System.out.println( "count="+m.count+" missive="+m.missive );
            }
        });
        dispatcher.start();
        sink.connect();
        
        dispatcher.subscribe(1, EchoResponse.class);
        
        EchoRequest request = new EchoRequest();
        int[] payload = new int[16000];
        for(int i = 0; i < payload.length; i++)
        {
            payload[i] = i;
        }
        request.missive = "Hello";
        request.payload = payload;
        
        long start = 0;
        long stop  = 0;
        int delay  = 1;
        for( int i = 0; i < (1000/delay)*1; i++ )
        {
            request.count = i;
            
            start = System.currentTimeMillis();
            dispatcher.publish(0, request);
            stop  = System.currentTimeMillis();
            System.out.println( "Diff="+(stop-start) );
            Thread.sleep(delay);
        }
        
        dispatcher.stop();
        sink.disconnect();
        

        
        
//        EchoRequest request = new EchoRequest();
//        int[] payload = new int[1048576/4];
//        for(int i = 0; i < payload.length; i++)
//        {
//            payload[i] = i;
//        }
//        request.missive = "HelloWorld";
//        request.payload = payload;
//        
//        ByteBuffer byteBuffer = ByteBuffer.allocate(1048576+28);
//        
//        ByteArrayOutputStream byteArray = new ByteArrayOutputStream(1048576+28);
//        
//        
//        long start = 0;
//        long stop  = 0;
//        for( int i = 0; i < 5; i++ )
//        {
//            request.count = i;
//            
//            byteArray.reset();
//            StreamOutputStream stream = new StreamOutputStream(byteArray);
//            //byteBuffer.clear();
//            //ByteBufferOutputStream block = new ByteBufferOutputStream(byteBuffer);
//            
//            start = System.currentTimeMillis();
//            request.marshal(stream);
//            //request.unmarshal(block);
//            
//            stop  = System.currentTimeMillis();
//            System.out.println( "Diff="+(stop-start) );
//            Thread.sleep(1000);
//            
//        }
        
        
//        for (int j = 0; j < 5; j++)
//        {
//            Thread[] threads = new Thread[Thread.activeCount()];
//            Thread.enumerate(threads);
//            for (int i = 0; i < threads.length; i++)
//            {
//                Thread thread = threads[i];
//                System.out.println("Thread: " + thread);
//            }
//            
//            Thread.sleep(5000);
//        }
        
        
    }
}